<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-input v-model="query.title" clearable placeholder="输入搜索标题" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        
        <el-select v-model="query.bannerType" clearable placeholder="类型" style="width: 150px;" class="filter-item" @change="queryTypeSelect">
          <el-option
            v-for="item in dict.banner_type"
            :key="item.id"
            :label="item.label"
            :value="item.value" />
        </el-select>
        <el-select v-model="query.websiteType" clearable placeholder="类别" style="width: 250px;" class="filter-item">
          <el-option
            v-for="item in queryOptionsList"
            :key="item.id"
            :label="item.label"
            :value="item.value" />
        </el-select>
        <el-select v-model="query.status" clearable placeholder="状态" style="width: 150px;" class="filter-item">
          <el-option
            v-for="item in dict.is_enable"
            :key="item.id"
            :label="item.label"
            :value="item.value" />
        </el-select>
       
        <rrOperation :crud="crud" />
        <el-button
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="crud.toAdd"
        >
            新增
        </el-button>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <!-- <crudOperation :permission="permission" /> -->
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="60%">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" />
          </el-form-item>
          <el-form-item label="图片URL">
              <MaterialList
                      v-model="imageUrlArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
              <label v-if="imagIcon">图片标注(3840*1040)</label>
              <label v-else>图片标注(240*240)</label>
          </el-form-item>
          <el-row>
            <el-col :span="7">
              <el-form-item label="类型" prop="bannerType">
                <el-select v-model="form.bannerType" clearable placeholder="类型" @change="typeSelect(2)" style="width: 150px">
                  <el-option
                    v-for="item in dict.banner_type"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="9">
              <el-form-item label="类别" prop="websiteType" v-show="isClassType">
                <el-select v-model="form.websiteType" clearable filterable placeholder="请选择" style="width: 200px">
                  <el-option
                    v-for="item in queryTypeOptions"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="是否跳转" prop="isUrl">
                <el-radio-group v-model="form.isUrl" @change="isUrlSelect">
                  <el-radio label="1">是</el-radio>
                  <el-radio label="0">否</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            
          </el-row> 
          <el-row>
            <el-col :span="10" v-show="isShowUrl">
              <el-form-item label="跳转链接">
                <el-input v-model="form.url"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="排序" prop="sort">
                <el-input v-model="form.sort" type="number"/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="状态" prop="status">
                <el-radio-group v-model="form.status">
                  <el-radio label="1">正常</el-radio>
                  <el-radio label="0">禁用</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('title')" prop="title" label="标题" />
          <el-table-column v-if="columns.visible('imageUrl')" prop="imageUrl" label="图片">
            <template slot-scope="scope">
              <a :href="scope.row.imageUrl" style="color: #42b983" target="_blank">
                <img :src="scope.row.imageUrl" alt="点击打开" class="table-img" />
              </a>
            </template>
          </el-table-column>
        <el-table-column v-if="columns.visible('bannerType')" prop="bannerType" label="banner类型">
          <template slot-scope="scope">
            {{ dict.label.banner_type[scope.row.bannerType] }}
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip v-if="columns.visible('websiteType')" prop="websiteType" label="类别">
          <template slot-scope="scope" >
            <label v-if="scope.row.bannerType =='0'">{{ dict.label.website_type[scope.row.websiteType] }}</label>
            <label v-else>{{ dict.label.website_type_01[scope.row.websiteType] }}</label>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('sort')" prop="sort" label="排序" />
        <el-table-column v-if="columns.visible('status')" prop="status" label="状态">
          <template slot-scope="scope">
            {{ dict.label.is_enable[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="添加时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
  
        <el-table-column v-permission="['admin','syWebsiteBanner:edit','syWebsiteBanner:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSyWebsiteBanner from '@/api/syWebsiteBanner'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '', url: 'api/syWebsiteBanner', sort: 'banner_type ,create_time,desc', crudMethod: { ...crudSyWebsiteBanner }})
const defaultForm = { id: null, title: null, imageUrl: null, content: null, bannerType: null, websiteType: null, isUrl: '0', url: null, sort: null, status: '1', createTime: null, updateTime: null }
export default {
  name: 'SyWebsiteBanner',
  components: { pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['banner_type', 'website_type','website_type_01', 'is_enable'],
  data() {
    return {
      imagIcon:true,
      isClassType:false,
      queryOptionsList:[],
      queryTypeOptions: [],
      isShowUrl:false,
      imageUrlArr:[],
      permission: {
        add: ['admin', 'syWebsiteBanner:add'],
        edit: ['admin', 'syWebsiteBanner:edit'],
        del: ['admin', 'syWebsiteBanner:del']
      },
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        bannerType: [
          { required: true, message: '类型不能为空', trigger: 'blur' }
        ],   
        websiteType: [
          { required: true, message: '类别不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'title', display_name: '标题' }
      ]
    }
  },
  watch: {
    "imageUrlArr":function(val) {
      if (val) {
        this.form.imageUrl = val.join(",");
      }
    },
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.title
        delete this.crud.params.bannerType
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      this.form.isUrl = form.isUrl.toString();
      this.form.status = form.status.toString();
      this.typeSelect(1);
      this.isUrlSelect();
      if (form.imageUrl && form.id) {
        this.imageUrlArr = form.imageUrl.split(",");
      }else{
        this.imageUrlArr=[]
      }
    },
    typeSelect(val){
      let that = this.form;
      if(that.bannerType == '0'){//0banner
        this.isClassType = true;
        this.imagIcon= true;
        this.queryTypeOptions = this.dict.website_type;
      }else if(that.bannerType == '1'){//1：二维码
        this.isClassType = true;
        this.imagIcon= false;
        this.queryTypeOptions = this.dict.website_type_01;
      }else{
        this.isClassType = false;
      }
      if(val =='2'){
        this.form.websiteType ='';
      }
    },
    queryTypeSelect(){
      let that = this;
      if(that.query.bannerType == '0'){//0banner
        this.queryOptionsList = this.dict.website_type;
      }else if(that.query.bannerType == '1'){//1：二维码
        this.queryOptionsList = this.dict.website_type_01;
      }else{
        that.query.websiteType ='';
        this.queryOptionsList = [];
      }
    },
    
    isUrlSelect(){
      let that = this.form;
      if(that.isUrl == '1'){//是否跳转（0：否1：是）
        this.isShowUrl = true;
      }else{
        this.isShowUrl = false;
      }
    }
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
